/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package codegen.ui.nbvisual.visual;

import java.awt.BasicStroke;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Stroke;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.QuadCurve2D;
import java.util.List;
import org.netbeans.api.visual.widget.ConnectionWidget;
import org.netbeans.api.visual.widget.Scene;
import codegen.graphnetwork.model.*;
/**
 *
 * @author cuong.nguyenmanh2
 */
public class CurveConnectionWidget extends ConnectionWidget {

    static final Stroke boldStroke = new BasicStroke(2f);

    public CurveConnectionWidget(Scene scene) {
        super(scene);
    }

    @Override
    protected void paintWidget() {
        //super.paintWidget();

        List<Point> points = getControlPoints();
        CodeGenGraphScene scene = (CodeGenGraphScene) getScene();
        if (getSourceAnchor() != null && getTargetAnchor() != null) {
            Value sourceValue = scene.findPinByAnchor(getSourceAnchor());
            Value targetValue = scene.findPinByAnchor(getTargetAnchor());
            //drawCurve(points, sourceValue.isInput, targetValue.isInput);
        } else {
            //drawCurve(points, true, true);
        }

        drawCurve(points, true, true);
    }

    void drawCurve(List<Point> points, boolean firstLeft, boolean lastLeft) {
        if (!points.isEmpty()) {
            Point first = points.get(points.size() - 1);
            Point last = points.get(0);
            Point mid = new Point((first.x + last.x) / 2, (first.y + last.y) / 2);
            Point midfirst = new Point(mid.x, first.y);
            //midfirst.x = (firstLeft) ? 20 : -20;
            Point midlast = new Point(mid.x, last.y);
            //midlast.x = (lastLeft) ? 20 : -20;
            Graphics2D g = getGraphics();
            //g.drawLine(first.x, first.y, last.x, last.y);
            g.setStroke(boldStroke);
            //QuadCurve2D.Float curve = new QuadCurve2D.Float(first.x, first.y, midfirst.x, midfirst.y, last.x, last.y);
            CubicCurve2D.Float curve = new CubicCurve2D.Float(first.x, first.y, midfirst.x, midfirst.y, midlast.x, midlast.y, last.x, last.y);
            g.draw(curve);
        }
    }
}
